const express =  require("express");
const isLoginMiddle = require('../middleware/isLoginMiddle')

var md5 = require('md5');
//引入校验文件
const addAdminReg = require("../middleware/addAdminReg")
//引入数据库集合对象
const adminModel = require("../db/model/adminModel")
//创建一个路由
const route = express.Router();
route.post("/addAdmin", isLoginMiddle,addAdminReg ,async (req, res) => {
    
    //获取数据
    const {
        adminName,
        password,
        regTime
    } = req.body;
    //查看是否有管理员信息
    const userinfoModel = await adminModel.findOne({
        adminName,
    })

    //判断管理员是否被注册
    if (userinfoModel) {
        return res.send({
            code: 10001,
            msg: "管理员-用户已经被注册",
            data: null
        })
    }

    //如果没有查询到,则向数据库添加该数据
    await adminModel.create({
        
        adminName,
        password : md5(password + "3388"),
        regTime
    })
    

    res.send({
        code: 10000,
        msg: "添加管理员-添加成功",
        data: null
    })

})

module.exports = route